<template>
  <view class="zui-thermometer-mercury">
    <view class="ztm-wrapper" :style="style">
      <Bar
        class="ztm-background"
        :width="width"
        :height="height"
        :border="border"
        :start-color="background[0]"
        :end-color="background[1]"
      />
      <Bar
        class="ztm-foreground"
        :value="value"
        :width="innerWidth"
        :height="innerHeight"
        :border="border"
        :start-color="foreground[0]"
        :end-color="foreground[1]"
      />
      <!-- <view class="ztm-value">{{ valueDisp }}<br v-if="isWrap" />{{ unit }}</view> -->
    </view>
  </view>
</template>

<script>
import Bar from "./zui-thermometer-mercury-bar";

export default {
  name: "zui-thermometer-mercury",

  components: { Bar },

  props: {
    width: {
      type: Number,
      default: 16,
    },
    height: {
      type: Number,
      default: 340,
    },
    foreground: {
      type: Array,
      default: ["rgb(232, 108, 255)", "rgb(58, 72, 255)"],
    },
    background: {
      type: Array,
      default: ["#d1d3d4", "#ffffff"],
    },
    value: {
      type: Number,
      default: 0,
    },
    unit: {
      type: String,
      default: "%",
    },
    /**
     * 单位换行
     *
     * @property {string} 'auto' | 'nowrap' | 'wrap'
     */
    wrap: {
      type: String,
      default: 'nowrap',
    }
  },

  data() {
    return {};
  },

  computed: {
    valueDisp() {
      return Math.round(this.value * 100);
    },

    isWrap() {
      if (this.wrap === 'wrap') return true
      if (this.wrap === 'nowrap') return false
      return String(this.valueDisp).length > 2
    },

    innerOffset() {
      return this.width * 0.1364;
    },
    innerWidth() {
      return this.width - this.innerOffset * 2;
    },
    innerHeight() {
      return this.height - this.innerOffset * 2 + this.border;
    },

    border() {
      return this.width * 0.01602;
    },

    style() {
      const style = {
        "--ztm-width": `${this.width}px`,
        "--ztm-height": `${this.height}px`,
        "--ztm-inner-width": `${this.innerWidth}px`,
        "--ztm-inner-height": `${this.innerHeight}px`,
        "--ztm-inner-offset": `${this.innerOffset}px`,
        "--ztm-unit-offset": this.isWrap ? '0.55' : '0.85'
      };

      return Object.keys(style)
        .map((key) => `${key}:${style[key]}`)
        .join(";");
    },
  },

  methods: {},
};
</script>

<style scoped lang="scss">
.zui-thermometer-mercury {
  --ztm-bg-start: #d1d3d4;
  --ztm-bg-end: #ffffff;
  --ztm-fg-start: #eb008c;
  --ztm-fg-end: #ef33a3;

  position: relative;
  display: inline-block;
}

.ztm-wrapper {
  position: relative;
  width: var(--ztm-width);
  height: var(--ztm-height);
}

.ztm-foreground {
  position: absolute;
  z-index: 50;
  bottom: var(--ztm-inner-offset);
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(137, 232, 217, 0.20);
}

.ztm-value {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  z-index: 100;
  width: var(--ztm-inner-width);
  height: var(--ztm-inner-width);
  bottom: calc(var(--ztm-inner-offset) * var(--ztm-unit-offset, 0.85));
  left: var(--ztm-inner-offset);
  color: #fff;
  text-align: center;
  font-size: calc(var(--ztm-width) * 0.25);
  line-height: 1em;
  text-shadow: 0.1em 0.1em rgba(0, 0, 0, 0.35);
}
</style>
